package com.fozu.office.sale.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fozu.common.config.Constants;
import com.fozu.common.pojo.ResultVO;
import com.fozu.common.util.IdGen;
import com.fozu.office.sale.entity.PredictVO;
import com.fozu.office.sale.service.PredictServiceImpl;
import com.fozu.office.system.entity.UserVO;
/*
 * 销售预测
 */
@Controller
@RequestMapping(value="predict")
public class PredictController {
	@Autowired
	private PredictServiceImpl predictService;
	
	//跳转到预测列表
	@RequestMapping(value="predictList.shtml")
	public String toPredictList(){
		return "sale/predict/predictList";
	}
	//跳转到查看页面
	@RequestMapping(value="predictQuery.shtml")
	public String toPredictQuery(){
		return "sale/predict/predictQuery";
	}
	//跳转到添加页面
	@RequestMapping(value="predictAdd.shtml")
	public String toPredictAdd(){
		return "sale/predict/predictAdd";
	}
	//获取预测列表(分页)
	@PostMapping(value="predictList")
	@ResponseBody
	public ResultVO predictList(
			@RequestParam(value="pageNo") Long pageNo,
			@RequestParam(value="pageSize") Long pageSize,
			@RequestParam(value="predictDate",required=false)String predictDate){
		try{
			PredictVO predict = new PredictVO();
			predict.setPredictDate(predictDate);
			IPage<PredictVO> page =predictService.listPredict(new Page<PredictVO>(pageNo,pageSize), predict);
			return new ResultVO(page);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"获取失败",null);
		}
	}
	//通过Id那对象
	@PostMapping(value="getPredictById")
	@ResponseBody
	public ResultVO getPredictById(@RequestParam(value="predictId", required=true) String predictId){
		try{
			PredictVO predict = new PredictVO();
			predict.setId(predictId);
			PredictVO pd =predictService.getPredictById(predict);
			return new ResultVO(pd);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_Exception,"系统异常",null);
		}
	}
	//添加
	@PostMapping(value="doAddPredict")
	@ResponseBody
	public ResultVO doAddPredict(HttpServletRequest request,
								 @RequestParam(value="title",required=true) String title,
								 @RequestParam(value="predictDate",required=true)String predictDate,
								 @RequestParam(value="result",required=true)String result,
								 @RequestParam(value="predictType",required=true)Integer predictType){
		try{
			String id = IdGen.getId();
			UserVO user = (UserVO) request.getSession().getAttribute("loginUser");
			PredictVO predict =new PredictVO(id,result,title,predictDate,predictType);
			predict.setCreateDate(new Date());
			predict.setCreateBy(user.getId());
			predict.setUpdateDate(new Date());
			predict.setUpdateBy(user.getId());
			predictService.addPredict(predict);
			return new ResultVO(Constants.status_SUCCESS,"添加成功",null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_Exception,"系统异常",null);
		}
	}
}
